<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\constant\v1\CommonConstant;
use plugin\admin\app\common\Util;
use plugin\admin\app\common\fast\Random;
use support\Db;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1RechargeCard;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 充值卡
 */
class V1RechargeCardController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 需要登录无需权的方法
     * @var array
     */
    protected $noNeedAuth = [
        'select'
    ];

    /**
     * @var V1RechargeCard
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1RechargeCard;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('v1-recharge-card/index');
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $amount = $request->input('amount');
            $number = $request->input('number');
            $remark = $request->input('remark');
            if (!$amount) {
                return $this->json(1, '请输入金额！');
            }
            if (!is_numeric($amount) || $amount < 1) {
                return $this->json(1, '金额不能小于1！');
            }
            if (!$number) {
                return $this->json(1, '请输入数量！');
            }
            if (!is_numeric($number) || $number < 1) {
                return $this->json(1, '数量不能小于1！');
            }

            $insert_data = [];
            $date = date('Y-m-d H:i:s');
            for ($i = 0; $i < $number; $i++) {
                $insert_data[] = [
                    'code' => Random::uuid(),
                    'amount' => $amount,
                    'remark' => $remark,
                    'created_at' => $date,
                ];
            }
            Util::db()->transaction(function () use ($insert_data) {
                $this->model->insert($insert_data);
            });
            return $this->json(0, 'ok');
//            return parent::insert($request);
        }
        return view('v1-recharge-card/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('v1-recharge-card/update');
    }

    /**
     * 删除
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $this->doDelete($ids);
        return $this->json(0);
    }

}
